var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
var httpWeike = 'http://192.168.15.41:8082/weike';
var httpNativetalk = 'http://192.168.15.41:8080/nativetalk';
var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
var classId, tea_id, pageNum = 1, classDataArr = [], totalPage;
Page({
  data: {
    imgUrl: 'http://file.waijiaojun.com/waijiaojun/icon',
  },
  onLoad: function (e) {
    if (app.globalData.token !== null) {
      tea_id = e.teaId
    }
    this.classData(pageNum)
  },
  //数据请求与处理
  classData: function (e) {
    if (app.globalData.token !== null) {
      // tea_id = e.teaId
      var that = this;
      wx.request({
        url: httpNativetalk + '/app/7/token/book/connect',
        data: {
          "currentPage": pageNum,
          "pageSize": 10,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          'token': app.globalData.token,
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
          totalPage = res.data.obj.totalPage
          if (res.data.obj == null) {
            classDataArr = '';
            that.setData({
              classDataArr: classDataArr
            })
          };
          if (res.data.obj.setmeals) {
            var setmealsData = res.data.obj.setmeals;
            classDataArr.push(setmealsData);
            for (var i = 0; i < setmealsData.length; i++) {
              setmealsData[i].set_picture = 'http://file.waijiaojun.com/' + setmealsData[i].set_picture;
              var Times = setmealsData[i].end_time / 86400000 - Date.parse(new Date()) / 86400000;
              var day = Times;
              setmealsData[i].day = Math.floor(day);
              var Mon = Math.floor(Times / 30);
              if (Mon > 0) {
                setmealsData[i].Times = Mon + '个月'
              } else {
                setmealsData[i].Times = Math.floor(Times) + '天'
              }
            };
            that.setData({
              // cou_id: setmealsData.cou_id,
              classDataArr: classDataArr,
              setmealsData: setmealsData
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  //跳转课程详情
  toClassDetailFun: function (e) {
    console.log(e)
    var classIds = e.currentTarget.dataset.idx;
    var day = e.currentTarget.dataset.day;
    if (day > 0) {

      if (tea_id == undefined) {
        var titleData = e.currentTarget.dataset.titles;
        wx.navigateTo({
          url: '/pages/classDetail/classDetail?classId=' + classIds,
        });
        wx.setNavigationBarTitle({
          title: titleData,
        })
      };
      if (tea_id !== undefined) {
        var dates = Date.now();
        wx.request({
          url: httpNativetalk + '/app/11/teacher/reservetime',
          data: {
            "cou_id": classIds,
            "date": Date.parse(new Date()),
            "tea_id": tea_id,
          },
          header: {
            "Timestamp": timeSecond,
            "SignInfo": SignInfoData,
            "content-Type": "application/json",
            "client_type": "3",
            "version": 15,
            'token': app.globalData.token,
          },
          method: "POST",
          success: function (res) {
            console.log(res);
            if (res.data.obj.courseSize > 0) {
              var classIdArr = [];
              var courses = res.data.obj.courses;
              for (var i = 0; i < courses.length; i++) {
                if (courses[i].cou_id == classIds) {
                  classIdArr.push(courses[i].cou_id);
                }
              };
              if (classIdArr.length == 1) {
                wx.navigateTo({
                  url: '/pages/selectTime/selectTime?teaId=' + tea_id + '&date=' + dates + '&classId=' + classIds,
                });
              } else {
                wx.showModal({
                  title: '温馨提示',
                  content: '该老师暂时没有开设此课',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              }
            }
            if (res.data.obj.courseSize == 0) {
              wx.navigateTo({
                url: '/pages/selectTime/selectTime?teaId=' + tea_id + '&date=' + dates + '&classId=' + classIds,
              });
            }
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      };
    } else {
      wx.showToast({
        title: '该课程已过期',
        icon: 'loading',
        duration: 2000
      })
    }
  },
  // 触底加载更多
  onReachBottom: function () {
    pageNum++;
    if (pageNum > 1 && pageNum <= totalPage) {
      this.classData(pageNum);
    }
  },
  onUnload: function (e) {
    pageNum = 1;
    classDataArr = [];
  },
})